﻿Imports System.Data.SqlClient
Imports System.Net.Mail


Partial Class forgotten_password_1
    Inherits System.Web.UI.Page

    Dim cn As New SqlConnection(ConfigurationSettings.AppSettings("conn"))



    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        lblPassword.Visible = False
        RadioButton1.Visible = False
        RadioButton2.Visible = False
        Button2.Visible = False

        lblquestion.Visible = False
        txtquestion.Visible = False
        Label4.Visible = False
        TextBox3.Visible = False
        Label6.Visible = False
        Button3.Visible = False
        lblEmail.Visible = False
        txtEmail.Visible = False
        revEmail.Visible = False
        Button4.Visible = False
        Label7.Visible = False

        Label1.Text = ""
        Label4.Text = ""
        Label6.Text = ""
        Label7.Text = ""


    End Sub

    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim cmd As New SqlCommand("select username from users where username='" + txtUserName.Text.ToLower + "'", cn)
        Try

            cn.Open()
            If cmd.ExecuteScalar = txtUserName.Text.ToLower Then
                lblUserName.Visible = False
                txtUserName.Visible = False
                Button1.Visible = False
                Label1.Visible = False

                lblPassword.Visible = True
                RadioButton1.Visible = True
                RadioButton2.Visible = True
                Button2.Visible = True
            Else
                Label1.Text = "Enter Valid User Name."
            End If
            cn.Close()
        Catch ex As Exception
            Label1.Text = "Enter Valid User Name."
        End Try





    End Sub

    Protected Sub Button2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button2.Click
        lblPassword.Visible = False
        RadioButton1.Visible = False
        RadioButton2.Visible = False
        Button2.Visible = False
        If RadioButton1.Checked Then
            lblEmail.Visible = True
            txtEmail.Visible = True
            revEmail.Visible = True
            Button4.Visible = True
            Label7.Visible = True
        ElseIf RadioButton2.Checked Then
            lblquestion.Visible = True
            txtquestion.Visible = True
            Label4.Visible = True
            TextBox3.Visible = True
            Label6.Visible = True
            Button3.Visible = True
            Try
                Dim cmd As New SqlCommand("select QUESTION from users where username='" + txtUserName.Text.ToLower + "'", cn)
                cn.Open()
                Dim dr As SqlDataReader = cmd.ExecuteReader
                dr.Read()
                txtquestion.Text = dr("QUESTION")

                cn.Close()

            Catch ex As Exception

            End Try
    



        End If
    End Sub

    Protected Sub Button4_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button4.Click
        Dim cmd As New SqlCommand("select username,FIRSTNAME,password,emailid from users where username='" + txtUserName.Text.ToLower + "'", cn)
        Dim dr As SqlDataReader
        Try
            cn.Open()
            dr = cmd.ExecuteReader
            dr.Read()
        Catch
        End Try

        Dim uname As String = dr("username")
        Dim password As String = dr("password")
        Dim email As String = dr("emailid")
        Dim FIRSTNAME As String = dr("FIRSTNAME")

        cn.Close()


        If txtEmail.Text.ToLower = email.ToLower Then
            ' do nothing
        Else
            Label7.Text = "Email ID is not valid as per your username."
            Exit Sub
        End If





        ' here i am sending mail to user
        Const googleMailPortNumber As Integer = 587
        Const userName As String = "rishi.patel008"
        Const pwd As String = "rishipat"
        Dim Mail As MailMessage
        Dim smtp As SmtpClient

        Dim from As String = "rishi.patel008@gmail.com"
        Dim too As String = email
        Dim subject As String = "Password Recovery"
        Dim body As String

        body += "<b><p class='MsoNormal'>Hi " + FIRSTNAME + "<span style='mso-spacerun:yes;'>&nbsp;</span>,</p>"

        body += "<p class='MsoNormal'>This is the email notification for your inquiry about forgot"
        body += "password.</p>"
        body += "<p class='MsoNormal'><o:p>&nbsp;</o:p></p>"
        body += "<p class='MsoNormal'>Username : </p><B>" + uname + "<B>"
        body += "<p class='MsoNormal'>Password : </p><B>" + password + "<B>"
        body += "<p class='MsoNormal'><o:p>&nbsp;</o:p></p>"
        body += "<p class='MsoNormal'>Please, Don’t share your private information (such as password"
        body += ")to others.</p>"
        body += "<p class='MsoNormal'><o:p>&nbsp;</o:p></p>"
        body += "<p class='MsoNormal'>Thank you,</p>"
        body += "<p class='MsoNormal'><a href='http://www.onlinecricket.com'>http://www.onlinecricket.com</a></p>"
        body += "<p class='MsoNormal'><o:p>&nbsp;</o:p></p>"
        body += "<p class='MsoNormal'><o:p>&nbsp;</o:p></p></b>"



        Mail = New MailMessage(from, too, subject, body)

        Mail.IsBodyHtml = True
        smtp = New SmtpClient("smtp.gmail.com", googleMailPortNumber)
        smtp.EnableSsl = True
        smtp.DeliveryMethod = SmtpDeliveryMethod.Network
        smtp.Credentials = New System.Net.NetworkCredential(userName, pwd)

        Try
            smtp.Send(Mail)
            Label7.Visible = True
            Label7.Text = "Your password has been sent to your Email address"



        Catch ex As Exception
            Label7.Text = "Problem occured while sending mail to your emailID."
        End Try


    End Sub

    Protected Sub Button3_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button3.Click
        Try
            Dim cmd As New SqlCommand("select answer from users where username='" + txtUserName.Text.ToLower + "'", cn)
            cn.Open()
            Dim dr As SqlDataReader = cmd.ExecuteReader
            dr.Read()
            Dim ans As String = dr("answer")
            cn.Close()

            If (ans.ToLower = TextBox3.Text.ToLower) Then
                Session("forgetusername") = txtUserName.Text
                Response.Redirect("forgotten password 2.aspx")
            Else
                Label6.Text = "Enter Correct Answer"
                Response.Redirect("forgotten password 1.aspx")
            End If
        Catch ex As Exception

        End Try
 
    End Sub
End Class
